<template>
  <label class="s-radio" :class="{'is-checked': label === model}">
    <span class="s-radio_input">
      <span class="s-radio_inner"></span>
      <input
        type="radio"
        class="s-radio_original"
        :name="name"
        v-model="model"
        :value="label"
      />
    </span>
    <span class="s-radio_label">
      <slot></slot>
      <template v-if="!$slots.default">{{ label }}</template>
    </span>
  </label>
</template>

<script>
export default {
  name: 's-radio',
  inject: {
    RadioGroup: {
      default: ''
    }
  },
  props: {
    label: {
      type: [String, Number, Boolean],
      default: ''
    }, // 这是用户自定义的 radio 的绑定值
    value: null, // 这是父组件 v-model 传递过来的
    name: {
      type: String,
      default: ''
    }
  },
  computed: {
    model: {
      get () {
        return this.isGroup ? this.RadioGroup.value : this.value
      },
      set (value) {
        this.isGroup ? this.RadioGroup.$emit('input', value) : this.$emit('input', value)
      }
    },
    isGroup () {
      return this.RadioGroup
    }
  }
}
</script>

<style lang="scss" scoped>
.s-radio {
  color: #606266;
  font-weight: 500;
  line-height: 1;
  font-size: 14px;
  position: relative;
  cursor: pointer;
  display: inline-block;
  white-space: nowrap;
  user-select: none;
  margin-right: 30px;
  -moz-user-select: none;
  -webkit-user-select: none;
  -ms-user-select: none;
  .s-radio_input {
    white-space: nowrap;
    cursor: pointer;
    outline: none;
    display: inline-block;
    position: relative;
    line-height: 1;
    vertical-align: middle;
    .s-radio_inner {
      display: inline-block;
      position: relative;
      border: 1px solid #dcdfe6;
      border-radius: 100%;
      box-sizing: border-box;
      width: 14px;
      height: 14px;
      background-color: #fff;
      &:after {
        content: '';
        width: 4px;
        height: 4px;
        border-color: #fff;
        background-color: #fff;
        border-radius: 100%;
        box-sizing: content-box;
        position: absolute;
        left: 50%;
        top: 1px;
        transform: translate(-50%, 70%) scale(0);
        transition: transform .15s ease-in;
      }
    }
    .s-radio_original {
      opacity: 0;
      outline: 0;
      position: absolute;
      left: 10px;
      margin: 0;
      width: 0;
      height: 0;
      z-index: -1;
    }
  }
  .s-radio_label {
    padding-left: 10px;
    font-size: 14px;
  }
}

.s-radio.is-checked {
  .s-radio_input {
    .s-radio_inner {
      background-color: #409eff;
      border-color: #409eff;
      &:after {
        transform: translate(-50%, 70%) scale(1)
      }
    }
  }
  .s-radio_label {
    color: #409eff;
  }
}
</style>
